package com.wzs.easytobuy.controller;

import com.wzs.easytobuy.model.Address;
import com.wzs.easytobuy.model.Order;
import com.wzs.easytobuy.model.ShoppingCar;
import com.wzs.easytobuy.model.User;
import com.wzs.easytobuy.service.ICarService;
import com.wzs.easytobuy.vo.PaginationVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author miaomiao
 * @version 1.0.0
 * @Description 处理购物车模块的控制器
 * @createTime 2021-06-29- 10:49:00
 */
@Controller
@RequestMapping("/car/")
public class CarController {

    @Resource
    private ICarService iCarService;

    @RequestMapping("BuyCar.do")
    public String BuyCar() {
        return "BuyCar";
    }

    @RequestMapping("showCar.do")
    @ResponseBody
    public List<ShoppingCar> showCar(String userId) {

        System.out.println("显示购物车列表");

        return iCarService.selectByUid(userId);
    }

    @RequestMapping("addProduct.do")
    @ResponseBody
    public Map<String, Object> addProduct(String productId, String userId, String price, String imageUrl, String sales, String number, String colorId, String sizeId) {

        System.out.println("执行添加商品到购物车的操作");

        int number1;

        if (number != null) {
            number1 = Integer.parseInt(number);
        } else {
            number1 = 1;
        }

        System.out.println(colorId);
        System.out.println(sizeId);

        try {
            Map<String, Object> map = new HashMap<>();
            if (colorId == null && sizeId == null) {
                boolean flag = iCarService.addProduct(productId, userId, price, imageUrl, sales, number1);
                map.put("success", flag);
            }
            if (colorId != null && sizeId != null) {
                boolean flag = iCarService.addProduct2(productId, userId, price, imageUrl, number1, colorId, sizeId);
                map.put("success", flag);
            }


            return map;
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            Map<String, Object> map = new HashMap<>();
            map.put("success", false);
            map.put("msg", msg);

            return map;
        }
    }

    @RequestMapping("pageList.do")
    @ResponseBody
    public PaginationVO<Map<String, Object>> pageList(String pageNo, String pageSize, String productName, String userId) {

        System.out.println("购物车列表分页显示 + 按商品名称条件查询");

        int pageNo1 = Integer.parseInt(pageNo);
        int pageSize1 = Integer.parseInt(pageSize);

        int skipCount = (pageNo1 - 1) * pageSize1;

        HashMap<String, Object> map = new HashMap<>();
        map.put("productName", productName);
        map.put("userId", userId);
        map.put("skipCount", skipCount);
        map.put("pageSize", pageSize1);

        return iCarService.pageList(map);
    }

    @RequestMapping("delete.do")
    @ResponseBody
    public boolean delete(String id) {

        System.out.println("执行删除购物车中商品的操作");

        return iCarService.deleteById(id);
    }

    @RequestMapping("Settlement.do")
    public String Settlement(HttpSession session, @RequestParam(value = "id") List<String> ids) {

        session.setAttribute("ids", ids);
        User u = (User) session.getAttribute("loginUser");
        //将设置的默认地址填充
        Address address = iCarService.selectAddressByUid(u.getId(), 1);

        session.setAttribute("address", address);

        return "BuyCar_Two";
    }

    @RequestMapping("carList.do")
    @ResponseBody
    public List<Map<String, Object>> carList(@RequestParam(value = "ids[]") List<String> ids) {
        return iCarService.carList(ids);
    }

    @RequestMapping("saveAddress.do")
    @ResponseBody
    public Map<String, Object> saveAddress(Address address) {
        return iCarService.saveAddress(address);
    }

    @RequestMapping("deal.do")
    public String deal(Model model,
                       @RequestParam("userId") String userId,
                       @RequestParam("addressId") String addressId,
                       @RequestParam("shopCarIds") List<String> shopCarIds,
                       @RequestParam("psMethod") String psMethod,
                       @RequestParam("amountPrice") String amountPrice) {

        List<Order> orderList = new ArrayList<>();
        String msg = "";
        for (String sid : shopCarIds) {
            Order order;
            try {
                order = iCarService.deal(psMethod, userId, addressId, sid,amountPrice);
                orderList.add(order);
            } catch (Exception e) {
                e.printStackTrace();
                msg = e.getMessage();
                model.addAttribute("msg", msg);
                return "BuyCar_Two";
            }
            if (order == null) {
                msg = "系统出现异常请重试";
                model.addAttribute("msg", msg);
                return "BuyCar_Two";
            }

        }
        model.addAttribute("orderList", orderList);


        return "BuyCar_Three";
    }
}
















